local sk__shouji = fk.CreateSkill {

  name = "sk__shouji",

  tags = {},

}

local SrYueyingMapper = function(id)
  local suit = Fk:getCardById(id, true).suit
  local mapper = {
    [Card.Spade] = "duel",
    [Card.Club] = "collateral",
    [Card.Heart] = "snatch",
    [Card.Diamond] = "fire_attack",
    [Card.NoSuit] = "",
  }
  return mapper[suit]
end

sk__shouji:addEffect("active", {
  name = "sk__shouji",
  anim_type = "control",
  prompt = function(self, player, selected_cards, selected_targets)
    if #selected_cards == 0 then
      return "#sk__shouji"
    elseif #selected_cards == 1 then
      local card = Fk:cloneCard(SrYueyingMapper(selected_cards[1]))
      if #selected_targets == 0 then
        return "#sk__shouji1:::" .. card.name
      elseif #selected_targets == 1 then
        return "#sk__shouji2:" .. selected_targets[1].id .. "::" .. card.name
      elseif #selected_targets == 2 then
        return "#sk__shouji3:" .. selected_targets[1].id .. ":" .. selected_targets[2].id .. ":" .. card.name
      end
    end
  end,
  card_num = 1,
  target_num = 2,
  can_use = function(self, player)
    return player:usedSkillTimes(sk__shouji.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and not player:prohibitDiscard(to_select)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if #selected < 2 and #selected_cards == 1 then
      local card = Fk:cloneCard(SrYueyingMapper(selected_cards[1]))
      card.skillName = sk__shouji.name
      local target = to_select
      if #selected == 0 then
        return target:canUse(card, { bypass_distances = true }) and not target:prohibitUse(card)
      elseif #selected == 1 then
        return selected[1]:canUseTo(card, target, { bypass_distances = true })
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local src = effect.tos[1]
    local target = effect.tos[2]
    room:throwCard(effect.cards, sk__shouji.name, player, player)
    local card = Fk:cloneCard(SrYueyingMapper(effect.cards[1]))
    card.skillName = sk__shouji.name
    if card.name == "collateral" then
      local targets = table.filter(Fk:currentRoom().alive_players, function(p)
        return target ~= p and target:inMyAttackRange(p)
      end)
      local to = src.id
      if #targets > 0 then
        to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
          "#sk__shouji-collateral:" .. src.id .. ":" .. target.id, sk__shouji.name, false)[1]
      end
      local use = {
        from = src,
        tos = { target, room:getPlayersByPos(to) },
        card = card,
      }
      room:useCard(use)
    else
      room:useVirtualCard(card.name, nil, src, target, sk__shouji.name)
    end
  end,
})

return sk__shouji
